<template>
  <el-dialog
    :visible.sync="centerDialogVisible"
    :before-close="closeAdd"
    width="40%"
    center
    :closeOnClickModal="false"
    :title="addTit"
  >
    <el-form :model="tabData" label-width="100px" class="demo-ruleForm" style="width:95%">
      <el-form-item label="新闻ID" prop="news_event_id " :style="addOr">
        <el-input v-model="tabData.news_event_id" readonly></el-input>
      </el-form-item>
      <el-form-item label="新闻标题" prop="news_event_title">
        <el-input v-model="tabData.news_event_title" :readonly="lookOr"></el-input>
      </el-form-item>
      <el-form-item label="新闻内容" prop="news_event_content ">
        <el-input type="textarea" v-model="tabData.news_event_content" :readonly="lookOr"></el-input>
      </el-form-item>
      <el-form-item
        style="margin-left: -100px!important;display: flex;justify-content: center;"
        :style="butNone"
      >
        <el-button type="primary" @click="submitForm">{{butTit}}</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
export default {
  props: ["newsShow"],
  data() {
    return {
      user: {},
      centerDialogVisible: false,
      addOr: "",
      addTit: "",
      butTit: "",
      addEdit: "",
      lookOr:"",
      butNone:"",
      tabData: {
        news_event_id: "",
        news_event_title: "",
        news_event_content: ""
      }
    };
  },
  watch: {
    newsShow: {
      handler(newVal, oldVal) {
        console.log(newVal);
        if (newVal[0] === 1) {
          this.centerDialogVisible = true;
          this.addEdit = newVal[1];
          this.tabData = newVal[2];
          if (newVal[1] == 0) {
            //0 是add  1 是edit 2 是查看
            this.addTit = "添加新闻";
            this.addOr = "display:none";
            this.butTit = "立即添加";
            this.butNone = "";
            this.lookOr = false;
          } else if (newVal[1] == 1) {
            //0 是add  1 是edit 2 是查看
            this.addTit = "修改新闻";
            this.addOr = "";
            this.butTit = "立即修改";
            this.butNone = "";
            this.lookOr = false;
          } else if (newVal[1] == 2) {
            //0 是add  1 是edit 2 是查看
            this.addTit = "查看新闻";
            this.addOr = "";
            this.lookOr = true;
            this.butNone = "display:none";
          }
        }
      }
    }
  },
  mounted() {
    this.user = JSON.parse(this.$cookie.get("userAdmin"));
  },
  methods: {
    //修改或者新增
    submitForm() {
      var that = this;
      if (that.addEdit == 0) {
        var parse = {
          admin_id: that.user.admin_id,
          content: that.tabData.news_event_content,
          title: that.tabData.news_event_title
        };
        //修添加新闻赛事
        var urlPath = "admin/addnewsevent";
      } else if (that.addEdit == 1) {
        var parse = {
          admin_id: that.user.admin_id,
          newsEventId: that.tabData.news_event_id,
          content: that.tabData.news_event_content,
          title: that.tabData.news_event_title
        };
        //修改新闻赛事
        var urlPath = "admin/updatenewsevent";
      }
      that.$axios
        .post("apis/" + urlPath, this.$qs.stringify(parse))
        .then(function(res) {
          if (res.status == 200) {
            if (res.data.status == 1) {
              that.centerDialogVisible = false;
              that.$emit("closeNews", 0);
              that.$message({
                message: that.addEdit == 0 ? "添加成功哦~" : "修改成功哦~",
                type: "success"
              });
            }
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    //关闭
    closeAdd() {
      this.centerDialogVisible = false;
      this.$emit("closeNews", 0);
    }
  }
};
</script>

<style lang="less" scoped>
.el-dialog__head {
  top: 10px !important;
  right: 10px !important;
}
</style>
